﻿using IdentityService.Domain.Repositories;
using IdentityService.Domian.Repositories;
using MediatR;

namespace IdentityService.Application.Commands
{
    public record AddMemberCommand(long roleId, IList<long> userIds) : IRequest<bool>;

    public class AddMemberCommandHandler : IRequestHandler<AddMemberCommand, bool>
    {
        private readonly IRoleRepository _roleRepository;
        private readonly IUserAccountRepository _userAccountRepository;
        public AddMemberCommandHandler(IRoleRepository roleRepository, IUserAccountRepository userAccountRepository)
            => (_roleRepository, _userAccountRepository) = (roleRepository, userAccountRepository);

        public async Task<bool> Handle(AddMemberCommand request, CancellationToken cancellationToken)
        {
            var role = await _roleRepository.FindByIdIncludeUserAccountsAsync(request.roleId);
            var accounts = await _userAccountRepository.FindByIdBlukAsync(request.userIds);
            
            if(role != null && accounts.Count > 0) 
                role.AddUserAccountRange(accounts);

            return true;
        }
    }
}
